package com.common.model.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;

/**
 * 返回结果
 * @Author: ccf
 */
@ApiModel("返回包装对象")
@Data
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Result<T> implements Serializable {
    @ApiModelProperty(value = "数据", dataType = "Object")
    private T datas;
    @ApiModelProperty(value = "状态", dataType = "int", example = "200")
    private Integer code;
    @ApiModelProperty(value = "信息", dataType = "String", example = "操作成功")
    private String msg;

    public static <T> Result<T> succeed(String msg) {
        return of(null, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> succeed(T model, String msg) {
        return of(model, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> succeed(T model) {
        return of(model, CodeEnum.SUCCESS.getCode(), "");
    }

    public static <T> Result<T> of(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    public static <T> Result<T> failed(String msg) {
        return of(null, CodeEnum.ERROR.getCode(), msg);
    }

    public static <T> Result<T> failed(T model, String msg) {
        return of(model, CodeEnum.ERROR.getCode(), msg);
    }


    public static <T> Result<T> status(boolean status, T model) {
        if(status) {
            return of(model, CodeEnum.SUCCESS.getCode(), "操作成功");
        } else {
            return of(model, CodeEnum.ERROR.getCode(), "操作失败");
        }
    }

    public static <T> Result<T> status(boolean status, T model, String successMsg, String errorMsg) {
        if(status) {
            return of(model, CodeEnum.SUCCESS.getCode(), successMsg);
        } else {
            return of(model, CodeEnum.ERROR.getCode(), errorMsg);
        }
    }
}
